<template>
  <input type="text" v-model="keyWord" />
  <h3>{{ keyWord }}</h3>
</template>

<script>
import { ref, customRef } from "vue";
export default {
  name: "App",
  setup(props) {
    // Vue提供的内置ref
    // let keyWord = ref(true);

    // 自定义ref
    let keyWord = myRef("hello", 500);
    function myRef(value, delay) {
      let timer;
      return customRef((track, trigger) => {
        return {
          get() {
            // 通知vue追踪数据变化
            track();
            return value;
          },
          set(newValue) {
            value = newValue;
            clearTimeout(timer);
            timer = setTimeout(() => {
              // 通知Vue重新解析模板
              trigger();
            }, delay);
          },
        };
      });
    }
    return {
      keyWord,
    };
  },
};
</script>

<style>
</style>
